package cn.ycc1.boot3fastjson.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author ycc
 * @date 2023/11/15
 */
@RestController
@Slf4j
public class TestController {

    @GetMapping("/test")
    public String test() {
        LocalDateTime now = LocalDateTime.now();
        // DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHmm");
        String formattedDateTime = now.format(formatter);
        System.out.println(formattedDateTime);
        return formattedDateTime;
    }

    @GetMapping("/test2")
    public String test2() throws UnsupportedEncodingException {
        String test = "{\"req\":[\"%3C%3Fxml+version%3D%221.0%22+encoding%3D%22GBK%22+standalone%3D%22yes%22%3F%3E%3Cxml%3E%3Ccurr_type%3ECNY%3C%2Fcurr_type%3E%3Cins_cd%3E08A9999999%3C%2Fins_cd%3E%3Cmchnt_cd%3E0002900F0370542%3C%2Fmchnt_cd%3E%3Cmchnt_order_no%3E2023111517433025517042%3C%2Fmchnt_order_no%3E%3Corder_amt%3E1%3C%2Forder_amt%3E%3Corder_type%3EFWC%3C%2Forder_type%3E%3Crandom_str%3EG7G4YHPQ5W9MEKLDFJ0DZCL47WAO96VV%3C%2Frandom_str%3E%3Creserved_addn_inf%3E%3C%2Freserved_addn_inf%3E%3Creserved_bank_type%3EALIPAYACCOUNT%3C%2Freserved_bank_type%3E%3Creserved_buyer_logon_id%3Eyic***%40126.com%3C%2Freserved_buyer_logon_id%3E%3Creserved_channel_order_id%3E2023111517433025517042%3C%2Freserved_channel_order_id%3E%3Creserved_coupon_fee%3E%3C%2Freserved_coupon_fee%3E%3Creserved_discount_goods_detail%3E%3C%2Freserved_discount_goods_detail%3E%3Creserved_fund_bill_list%3E%5B%7B%22amount%22%3A%220.01%22%2C%22fundChannel%22%3A%22ALIPAYACCOUNT%22%7D%5D%3C%2Freserved_fund_bill_list%3E%3Creserved_fy_settle_dt%3E20231115%3C%2Freserved_fy_settle_dt%3E%3Creserved_fy_trace_no%3E110134372021%3C%2Freserved_fy_trace_no%3E%3Creserved_is_credit%3E0%3C%2Freserved_is_credit%3E%3Creserved_risk_info%3E%3C%2Freserved_risk_info%3E%3Creserved_settlement_amt%3E1%3C%2Freserved_settlement_amt%3E%3Creserved_sub_mchnt_cd%3E2088231677317661%3C%2Freserved_sub_mchnt_cd%3E%3Creserved_voucher_detail_list%3E%3C%2Freserved_voucher_detail_list%3E%3Cresult_code%3E000000%3C%2Fresult_code%3E%3Cresult_msg%3ESUCCESS%3C%2Fresult_msg%3E%3Csettle_order_amt%3E1%3C%2Fsettle_order_amt%3E%3Csign%3EdP3l7ieHknHT6mxbV%2FRw%2FH8ZHBNz13qOEaXtiwhoaRpDtDyHoAv3bSGVYrrynQvyudedWxVxIxQL2UFaKLWZZ5C6iVWvSQJuq03KhJaprJXl7jbYt5lvm0M5lY6B%2FUscq173P4LKE7ERz27EykfO84OAmsdyuy12siwHB28aKFs%3D%3C%2Fsign%3E%3Cterm_id%3E%3C%2Fterm_id%3E%3Ctransaction_id%3E2023111522001458591415487523%3C%2Ftransaction_id%3E%3Ctxn_fin_ts%3E20231115174337%3C%2Ftxn_fin_ts%3E%3Cuser_id%3E2088902476758590%3C%2Fuser_id%3E%3C%2Fxml%3E\"]}";
        String jsonData2 = "{\"req\":[\"%3C%3Fxml+version%3D%221.0%22+encoding%3D%22GBK%22+standalone%3D%22yes%22%3F%3E%3Cxml%3E%3Ccurr_type%3ECNY%3C%2Fcurr_type%3E%3Cins_cd%3E08A9999999%3C%2Fins_cd%3E%3Cmchnt_cd%3E0002900F0370542%3C%2Fmchnt_cd%3E%3Cmchnt_order_no%3E2023111518434847888955%3C%2Fmchnt_order_no%3E%3Corder_amt%3E1%3C%2Forder_amt%3E%3Corder_type%3EFWC%3C%2Forder_type%3E%3Crandom_str%3E9YYU5XI4Q9ED0M9I7IO9PYWW9UERX6XT%3C%2Frandom_str%3E%3Creserved_addn_inf%3E%3C%2Freserved_addn_inf%3E%3Creserved_bank_type%3EALIPAYACCOUNT%3C%2Freserved_bank_type%3E%3Creserved_buyer_logon_id%3Eyic***%40126.com%3C%2Freserved_buyer_logon_id%3E%3Creserved_channel_order_id%3E2023111518434847888955%3C%2Freserved_channel_order_id%3E%3Creserved_coupon_fee%3E%3C%2Freserved_coupon_fee%3E%3Creserved_discount_goods_detail%3E%3C%2Freserved_discount_goods_detail%3E%3Creserved_fund_bill_list%3E%5B%7B%22amount%22%3A%220.01%22%2C%22fundChannel%22%3A%22ALIPAYACCOUNT%22%7D%5D%3C%2Freserved_fund_bill_list%3E%3Creserved_fy_settle_dt%3E20231115%3C%2Freserved_fy_settle_dt%3E%3Creserved_fy_trace_no%3E110134372089%3C%2Freserved_fy_trace_no%3E%3Creserved_is_credit%3E0%3C%2Freserved_is_credit%3E%3Creserved_risk_info%3E%3C%2Freserved_risk_info%3E%3Creserved_settlement_amt%3E1%3C%2Freserved_settlement_amt%3E%3Creserved_sub_mchnt_cd%3E2088231677317661%3C%2Freserved_sub_mchnt_cd%3E%3Creserved_voucher_detail_list%3E%3C%2Freserved_voucher_detail_list%3E%3Cresult_code%3E000000%3C%2Fresult_code%3E%3Cresult_msg%3ESUCCESS%3C%2Fresult_msg%3E%3Csettle_order_amt%3E1%3C%2Fsettle_order_amt%3E%3Csign%3EWLWgX3M1BZ2W%2F%2FKYjHIMoTNAYtOcZb0%2Bafw38cmpCJDwycZuaSCk8Q1wBCzma6D%2BSTi4alAnRoTNqyrgojHgr7jBu59HXP6mBf3jK2Xa760ZdzHwOYcCB879Pvcvw69AJBZ%2F7ct6BMP8%2FFiGKL0CY3gHdtmUKrkpuH742SFhb78%3D%3C%2Fsign%3E%3Cterm_id%3E%3C%2Fterm_id%3E%3Ctransaction_id%3E2023111522001458591419405101%3C%2Ftransaction_id%3E%3Ctxn_fin_ts%3E20231115184356%3C%2Ftxn_fin_ts%3E%3Cuser_id%3E2088902476758590%3C%2Fuser_id%3E%3C%2Fxml%3E\"]}";
//        log.info("fuiou notify: ");
//        JSONObject jsonObject = JSON.parseObject(jsonData2);
//        // log.info(jsonObject.toJSONString());
//        System.out.println(jsonObject);
//        String[] xmlData = jsonObject.getJSONArray(jsonData2); // 获取返回的xml报文
//        System.out.println(xmlData);
//        log.info(xmlData);
        JSONObject jsonObject = JSON.parseObject(jsonData2);
        JSONArray reqArray = jsonObject.getJSONArray("req");
        for (int i = 0; i < reqArray.size(); i++) {
            String reqStr = reqArray.getString(i);
            System.out.println("req[" + i + "]: " + reqStr);
            String rspXml = URLDecoder.decode(reqStr, "GBK");
            System.out.println(rspXml);
        }
        log.info("test write log");

        return "hello world";
    }

    @GetMapping("/test3")
    public String test3() {
        // 假设有一个JSON字符串
        String jsonString = "{\"name\":\"张三\", \"age\":30, \"isStudent\":false}";

        // 解析JSON字符串为一个JSONObject对象
        JSONObject jsonObject = JSON.parseObject(jsonString);

        // 获取name字段的值
        String name = jsonObject.getString("name");
        System.out.println("Name: " + name);

        // 获取age字段的值，注意这里会将其作为int类型处理
        int age = jsonObject.getIntValue("age");
        System.out.println("Age: " + age);

        // 获取isStudent字段的值，并将其作为boolean类型处理
        boolean isStudent = jsonObject.getBooleanValue("isStudent");
        System.out.println("Is Student: " + isStudent);

        String name2 = jsonObject.getString("name2");
        System.out.println("Name2: " + name2);
        if(StringUtils.isEmpty(name2)) {
            System.out.println("字符串为is empty");
        } else {
            System.out.println("字符串 not empty");
        }
        return name2;
    }

    @GetMapping("/test4")
    public String test4() {
        System.out.println("test4");
        return "test baidu comate";
    }
}
